package com.woniu.idearbac.Service.impl;

import com.woniu.idearbac.Service.PermissionService;
import com.woniu.idearbac.dto.PerDTO;
import com.woniu.idearbac.mapper.SysPermissionMapper;
import com.woniu.idearbac.vo.PermissionVo;
import com.woniu.idearbac.vo.TreeNode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 功能描述
 *
 * @author PC
 * @date -2022/11/29-时间-13:17
 * @product idea-rbac
 */
@Service
public class PermissionServiceImpl implements PermissionService {
    @Resource
    private SysPermissionMapper sysPermissionMapper;


    @Override
    public List<String> selectPermissionByTel(String tel) {
        return sysPermissionMapper.selectPermissionByTel(tel);
    }

    @Override
    @Transactional
    public int add(PerDTO perDTO) {
        sysPermissionMapper.del(perDTO.getRid());
        return sysPermissionMapper.add(perDTO);
    }

    @Override
    public List<TreeNode> selectAllPer(Integer pid) {
        List<TreeNode> nodes = sysPermissionMapper.selectAllPer(pid);
        if (nodes != null) {
            for (TreeNode node : nodes) {
                List<TreeNode> list = selectAllPer(node.getId());
                node.setChildren(list);
            }
        }
        return nodes;
    }

    @Override
    public List<Integer> selectPerByRid(Integer rid) {
        return sysPermissionMapper.selectPerByRid(rid);
    }

    @Override
    public List<PermissionVo> selMenu(String tel, Integer pid) {
        List<PermissionVo> permissionVos = sysPermissionMapper.selMenu(tel, pid);
        if (permissionVos != null) {
            for (PermissionVo vo : permissionVos) {
                List<PermissionVo> vos = selMenu(tel, vo.getId());
                vo.setChildren(vos);
            }
        }
        return permissionVos;
    }
}
